<?php
namespace Managers;

use Catalog\Categories;

class Tovar extends BaseManager
{
        protected $__table = '\Catalog\Items';


//    public function GetTovarById($id)
//    {
//       // $table_name = "brend";
//
//        $query = 'select * from clothing c, brend b, `type` t where c.id_brend = b.id_brend and c.id_type = t.id_type and c.isdel = 0 and c.name ='."'".$name."'";
//
//        $em = $this->getEntityManager();
//        $res = $em->getConnection()->executeQuery($query)->fetchAll();
//	    return $res[0];
//    }




        public function getTovarById($id) {
           $result = $this->_em->createQueryBuilder()
                    ->select('i')
                    ->from('\Catalog\Items', 'i')
                    ->where('i.id = :myid')->setParameter('myid', $id)
                    ->getQuery()
                    ->getArrayResult();
           return $result[0];
        }
        public function getTovarsByUserId($id) {
           // die($id);
           $result = $this->_em->createQueryBuilder()
                    ->select('i')
                    ->from('\Catalog\Items', 'i')
                    ->where('i.user = :userid')->setParameter('userid', $id)
                    ->getQuery()
                    //->getSQL();
                    ->getArrayResult();
            print_r($result);
            die;
           return $result;
        }

        public function addTovar($name, $description, $image_id){

            $tovar = new \Catalog\Items();
            $tovar->setName($name);
            $tovar->setDescription($description);
            $tovar->setPhotoUrl('');
            $tovar->setData(new \DateTime());
            $tovar->setViews(1);
            $tovar->setUser($this->_em->getReference('\Catalog\User', 1));
          //  print_r($image_id);
          //  die;
            $image = $this->_em->find('\Catalog\Images', $image_id);
            $tovar->setImage($image);
         //
            $this->_em->persist($tovar);
            $this->_em->flush();
           // }
          //  catch (\Exception $e){
          //      print_r($e);die('oo');
          //  }

            return $tovar;
        }

    public function updateImage($id, $url){
        $tovar = $this->_em->find($this->__table, $id);
        $tovar->setPhotoUrl($url);
        $this->_em->persist($tovar);
            $this->_em->flush();

     }


        public function addImage($name){
// die($name);
            $tovar = new \Catalog\Images();
            $tovar->setName($name);
            $this->_em->persist($tovar);
            $this->_em->flush();
            return $tovar->getId();
           // die('ok');
        }

//        public function addImage($name){
//            $tovar = new \Catalog\Images();
//            $tovar->setName($name);
//            $this->_em->persist($tovar);
//            $this->_em->flush();
//            return $tovar->getId();
//           // die('ok');
//        }
        public function getDescriptionByTovarId($id) {

           $result = $this->_em->createQueryBuilder()
                    ->select('i')
                    ->from('\Catalog\Items', 'i')
                    ->where('i.id = :myid')->setParameter('myid', $id)
                    ->getQuery()
                    ->getArrayResult();

          return $result[0];

        }

        public function getTovarbyTags() {
         $result = $this->_em->createQueryBuilder()
                    ->select('tt_alias', 'tov_alias')
                   // ->leftJoin('tt_alias.tag', 'tg_alias' )
                    ->leftJoin('tt_alias.tovar', 'tov_alias' )
                    ->from('\Catalog\TovarToTags', 'tt_alias')
             //       ->orderBy('tt.id', 'DESC')
           //          ->setMaxResults( 2 )
         //           ->setFirstResult( 1 )
                    ->getQuery()
                    ->getArrayResult();

         return $result;
        }
    public function insertTovar(){
        $tovar = new \Catalog\Tovar();
        $tovar->setName('newas');
        $tovar->setDescription('some');

        $this->_em->persist($tovar);
        $this->_em->flush();

    }
    public function delete($id) {
        $id=5;
		$entity = $this->_em->find($this->__table, $id);

		if($entity){
			$this->_em->remove($entity);
			$this->_em->flush();
		}

	}

    public function updateFromArray($id) {
		$entity = $this->_em->find($this->__table, $id);
        $entity->setName('newname');
       
        $this->_em->persist($entity);
        $this->_em->flush();
    }
    public function UpdateDescriptionById($id, $elm1)
    {
        $entity = $this->_em->find($this->__table, $id);
        $entity->setDescription($elm1);

        $this->_em->persist($entity);
        $this->_em->flush();

    }

    public function getMaster($id){
           $result = $this->_em->createQueryBuilder()
                    ->select('i','u')
                    ->from('\Catalog\Items', 'i')
                    ->innerJoin('i.user',"u")
                    ->where('i.id = :myid')->setParameter('myid', $id)
                    ->getQuery()
                    ->getArrayResult();
           
           return $result;

    }

}